package com.wr.service.admin.interceptor;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.wr.common.entity.TbSysUser;
import com.wr.common.utils.CookieUtils;
import com.wr.common.web.constants.WebConstants;
import com.wr.common.web.utils.HttpServletUtils;
import com.wr.service.admin.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class WebAdminInterceptor implements HandlerInterceptor {

    @Value("${itoken.sso}")
    private String sso;
    @Autowired
    private RedisService redisService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 没有token  肯定没有登录
        String token = CookieUtils.getCookieValue(request, WebConstants.SESSION_TOKEN);
        if (StrUtil.isBlank(token)){
            response.sendRedirect(String.format("%s/login?url=%s",sso, HttpServletUtils.getFullPath(request)));
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HttpSession session = request.getSession();
        TbSysUser tbSysUser = (TbSysUser)session.getAttribute(WebConstants.SESSION_USER);

        //已经登录的状态
        if (tbSysUser!=null && modelAndView != null){
            modelAndView.addObject(WebConstants.SESSION_USER,tbSysUser);
        }
        // 未登录状态
        else {
            String token = CookieUtils.getCookieValue(request, WebConstants.SESSION_TOKEN);
            // 临界点  怕刚进来的时候有  到这里没有了
            if (StrUtil.isNotBlank(token)){
                String loginCode = redisService.get(token);
                System.out.println(loginCode);
                if (StrUtil.isNotBlank(loginCode)){
                    String json = redisService.get(loginCode);
                    if (StrUtil.isNotBlank(json)){
                        // 说明有登录信息，已登录状态  创建局部会话
                        tbSysUser = JSONArray.parseObject(json, TbSysUser.class);
                        if (modelAndView != null){
                            modelAndView.addObject(WebConstants.SESSION_USER,tbSysUser);
                        }
                        request.getSession().setAttribute(WebConstants.SESSION_USER,tbSysUser);
                    }
                }
            }
        }

        // 二次确认是否有用户信息
        if (tbSysUser == null){
            response.sendRedirect(String.format("%s/login?url=%s",sso, HttpServletUtils.getFullPath(request)));
        }

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
